package com.dd.controller;

import com.dd.pojo.Course;
import com.dd.pojo.Vip;
import com.dd.service.VipService;
import com.dd.service.VipServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/vip")
public class VipController {
    @Autowired
    VipService vipService;

    //添加
    @RequestMapping("addvip")
    public String addvip(Vip vip) {

        vipService.addvipuser(vip);
        return "1";
    }

    //查询
    @RequestMapping("selectvip")
    public String selectvip() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<Vip> vips = vipService.selectVip();
        String str = mapper.writeValueAsString(vips);
        String jso = "{\"code\":0,\"msg\":\"\",\"count\":" + 25 + ",\"data\":" + str + "}";
        return jso;
    }

    //修改
    @RequestMapping("updatevip")
    public String updatevip(Vip vip) {
        vipService.updateVipById(vip);
        return "1";
    }

    //删除
    @RequestMapping("delectvip")
    public String deletevip(int id) {
        vipService.daletevipById(id);
        return "1";
    }

    @RequestMapping("/selectbyname")
    public String selectbykname(String name) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<Vip> selectbyname = vipService.selectbyname(name);
        String str = mapper.writeValueAsString(selectbyname);
        String n ="{\"code\":0,\"msg\":\"\",\"count\":" + 25 + ",\"data\":" + str + "}";
        return n;
    }

}
